<?php defined('BASEPATH') OR exit('No direct script access allowed');
if ( ! function_exists('img_detail') ) {
	function img_detail($filename,$path_new,$box_w, $box_h) {
		$result = base_url('uploads/no_img.png');
		$thumbs     = "product/";
		$image 		=  $path_new;
		//if(is_file($image))
			//return base_url($image);
		//echo $filename;
		//$folder = explode('/',$filename);
		//if( !is_dir('uploads/shop/product/'.$folder[0]) ){
			//mkdir('uploads/shop/product/'.$folder[0], 0777);
		//}
							
		//$filename = 'uploads/shop/p/'.$filename;
		//$filename = 'uploads/img.jpg';
		if(!is_file($filename))
			return $result;
		$image_info = getimagesize($filename);
		$image_type = $image_info[2];
		if( $image_type == IMAGETYPE_JPEG ) {
		   $img = imagecreatefromjpeg($filename);
		} elseif( $image_type == IMAGETYPE_GIF ) {
		   $img = imagecreatefromgif($filename);
		} elseif( $image_type == IMAGETYPE_PNG ) {
		   $img = imagecreatefrompng($filename);
		}
		 
		//create the image, of the required size
		$new = imagecreatetruecolor($box_w, $box_h);
		if($new === false) {
			//creation failed -- probably not enough memory
			return null;
		}
		
		//Fill the image with a light grey color
		//(this will be visible in the padding around the image,
		//if the aspect ratios of the image and the thumbnail do not match)
		//Replace this with any color you want, or comment it out for black.
		//I used grey for testing =)
		$fill = imagecolorallocate($new, 255, 255, 255);
		imagefill($new, 0, 0, $fill);
	
		//compute resize ratio
		$hratio = $box_h / imagesy($img);
		$wratio = $box_w / imagesx($img);
		$ratio = min($hratio, $wratio);
	
		//if the source is smaller than the thumbnail size, 
		//don't resize -- add a margin instead
		//(that is, dont magnify images)
		if($ratio > 1.0)
			$ratio = 1.0;
	
		//compute sizes
		$sy = floor(imagesy($img) * $ratio);
		$sx = floor(imagesx($img) * $ratio);
	
		//compute margins
		//Using these margins centers the image in the thumbnail.
		//If you always want the image to the top left, 
		//set both of these to 0
		$m_y = floor(($box_h - $sy) / 2);
		$m_x = floor(($box_w - $sx) / 2);
		//Copy the image data, and resample
		//
		//If you want a fast and ugly thumbnail,
		//replace imagecopyresampled with imagecopyresized
		if(!imagecopyresampled($new, $img,
			$m_x, $m_y, //dest x, y (margins)
			0, 0, //src x, y (0,0 means top left)
			$sx, $sy,//dest w, h (resample to this size (computed above)
			imagesx($img), imagesy($img)) //src w, h (the full size of the original)
		) {
			//copy failed
			imagedestroy($new);
			imagedestroy($img);
			//return null;
		}
		imagedestroy($img);
		if( $image_type == IMAGETYPE_JPEG ) {
		   imagejpeg($new,$image, 100);
		} elseif( $image_type == IMAGETYPE_GIF ) {
		   imagegif($new,$image);
		} elseif( $image_type == IMAGETYPE_PNG ) {
		   imagepng($new,$image);
		}
		return base_url($image);
	}
}